# Maintainer: Alexey Pavlov <Alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

pkgbase=python
pkgname=('python' 'python-devel')
pkgver=3.8.7
pkgrel=1
_pybasever=${pkgver%.*}
pkgdesc="Next generation of the python high-level scripting language"
arch=('i686' 'x86_64')
license=('custom')
url="https://www.python.org/"
makedepends=('libbz2-devel' 'libcrypt-devel' 'libexpat-devel'  'mpdecimal-devel' 'libsqlite-devel' 'libffi-devel' 'ncurses-devel' 'libreadline-devel' 'liblzma-devel' 'openssl-devel' 'zlib-devel')
source=(https://www.python.org/ftp/python/${pkgver%rc*}/Python-${pkgver}.tar.xz
        001-3.1-enable-new-dtags.patch
        002-3.2-getpath-exe-extension.patch
        003-3.4-select-cygwin.patch
        005-3.7-ctypes-cygwin.patch
        006-3.7-ftm.patch
        007-3.8-dbm-cygwin.patch
        008-3.8-distutils-cygwin.patch
        009-3.8-export-PySignal_SetWakeupFd.patch
        010-3.8-nis-cygwin.patch
        011-3.8-parser-cygwin.patch
        012-3.8-pep3149-cygwin.patch
        013-3.8-tkinter-cygwin.patch
        014-3.8-xxsubinterpreters-cygwin.patch
        015-rpath.patch
        016-no-static-lib.patch
        025-MINGW-compiler-customize-mingw-cygwin-compilers.patch
        026-3.7-mpdec-msys.patch
        027-install-import-library.patch
        900-msysize.patch
        920-allow-win-drives-in-os-path-isabs.patch
        930-fix-missing-tcp-include.patch
        940-rebase-python-dll.patch
        950-rebase-dlls.patch
        960-fix-parallel-make.patch)
sha256sums=('ddcc1df16bb5b87aa42ec5d20a5b902f2d088caa269b28e01590f97a798ec50a'
            'be96ddaca58a39ddaf1e3e5bb7900b34c0e6d00e9b64c4e0f8a3565a74a44e84'
            '4044827af3cf0e91a8ddd44cd588f17ea4e17619512edd299b5688ec6e112ad4'
            '82cfafc5b31ad4c9bb4c9786044c39c75762dbc2656abdfdc433c23fee69c02f'
            'f0bb75ca69c63894fc43e0f8218c9dbcc746935bf5ea095a724e6fb2f5dcc566'
            '098a756dfdb69b43bece4efe9d53a4f6ffc05a9a19a3d48854de787e69fc453c'
            'dc044b0651a67f7c76e4119d65085e42277c0513b8e9653f0972d955c3dde2c5'
            '8c460bf9522e50e06aa2c21a921eb42543d7a4cac6c598b13be43940a96966bf'
            '70f854f00de71372e49f2ebbff7491e09e9e036e8e3f3646fe2984e30fb4a9da'
            '62588723eb4051dccd0c8ab4183455e7ab99a3dca6c3c07e7403e1919b934ef0'
            'd6b15b6624183d64fc77e5a4e5d100f05266ac8a83fc4e21262c7969e2abf4e5'
            '9b84b5e027ff39ed3f827e805079f260db319a78571012815da30765ee2ceabc'
            '4c6d9d786da1673208b0ef2bc3340e1acf08ce861b49193eb1592c53c3a4bd26'
            'e48c719be36cb266ac3ceae834739fb1faae6740524cd672fd367f9304894a39'
            '437332e1a7195dd0adcd505ecdeab5c3a7a793efdd7fea7bec55f97957bb8934'
            '43aac098b3a6e2b7806653e51f923b25b7afbf93e0e01c94846c5f749a81f5de'
            '5b1083e9b50e149d623d863dee38ac1fb8d142f1bb78c8a01dcb09bfd97f4118'
            'fc6164a90f0ca2a9341aaf4448b4334c3393459ca5cbad6d7149f8bcf70da5fe'
            'b4042475c5c75e0b4c7c08ccad2891eb8098c066c5ba524a988a0519102e8e5d'
            'f1a1e381ece51e2531cb71002fe2e1a9642b1846d55055657c4e4aefda60b6da'
            '387a2b7931fb4958e2526991760d85677f44fa13cff0aeb0f41a267f1f7fd214'
            '17e4ac1b5f0fa8a6c410fb80d1ad99ec9185efef51b8450a31932b553c354ed1'
            'b439ff4f0a1f13858f8fb596414b74ed2c14fc3103d90287bb8e461ee89288b9'
            '612bc5bcbe24c2f623713172d318c65a69ae601d72bcc5a170fe7c4eee458d18'
            'c59025ca9d1219bbb42d620ca8d19398610737c5754f1873db07d55c47b2dad1')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      rm -rf $_fname
    fi
  done
}

prepare() {
  cd "${srcdir}/Python-${pkgver}"

  # FS#23997
  sed -i -e "s|^#.* /usr/local/bin/python|#!/usr/bin/python|" Lib/cgi.py

  apply_patch_with_msg \
    001-3.1-enable-new-dtags.patch \
    002-3.2-getpath-exe-extension.patch \
    003-3.4-select-cygwin.patch \
    005-3.7-ctypes-cygwin.patch \
    006-3.7-ftm.patch \
    007-3.8-dbm-cygwin.patch \
    008-3.8-distutils-cygwin.patch \
    009-3.8-export-PySignal_SetWakeupFd.patch \
    010-3.8-nis-cygwin.patch \
    011-3.8-parser-cygwin.patch \
    012-3.8-pep3149-cygwin.patch \
    013-3.8-tkinter-cygwin.patch \
    014-3.8-xxsubinterpreters-cygwin.patch \
    015-rpath.patch \
    016-no-static-lib.patch \
    025-MINGW-compiler-customize-mingw-cygwin-compilers.patch \
    026-3.7-mpdec-msys.patch \
    027-install-import-library.patch \
    900-msysize.patch \
    920-allow-win-drives-in-os-path-isabs.patch \
    940-rebase-python-dll.patch \
    950-rebase-dlls.patch \
    960-fix-parallel-make.patch
  
  # https://bugs.python.org/issue41374
  apply_patch_with_msg \
    930-fix-missing-tcp-include.patch

  # Incomplete patch from Ray Donnelly
  # patch -p1 -i ${srcdir}/3.3.2-allow-windows-paths-for-executable.patch

  # Ensure that we are using the system copy of various libraries (expat, zlib and libffi),
  # rather than copies shipped in the tarball
  rm -r Modules/expat
  rm -r Modules/_ctypes/{darwin,libffi}*
  rm -r Modules/_decimal/libmpdec
  autoreconf -fiv
}

build() {
  cd "${srcdir}/Python-${pkgver}"

  CPPFLAGS+=" -I/usr/include/ncursesw"
  LDFLAGS+=" -L."

  CXX=/usr/bin/g++ \
  ./configure \
    --prefix=/usr \
    --build=${CHOST} \
    --host=${CHOST} \
    --enable-shared \
    --enable-ipv6 \
    --with-dbmliborder=gdbm \
    --with-libc= \
    --with-libm= \
    --with-system-expat \
    --with-system-ffi \
    --with-system-libmpdec \
    --enable-loadable-sqlite-extensions \
    --without-ensurepip \
    ac_cv_func_bind_textdomain_codeset=yes

  LC_CTYPE=en_US.UTF-8 make EXTRA_CFLAGS="$CFLAGS" > >(tee make-python.log)
  sed -ne '/^\s*Failed to build these modules/,$ {p; /^\s*$/q42}' make-python.log
}

check() {
  cd "${srcdir}/Python-${pkgver}"
  "${srcdir}/Python-${pkgver}/python.exe" -m test.regrtest -x test_posixpath test_logging
}

package_python() {
  #optdepends=('tk: for tkinter' 'sqlite')
  depends=('libbz2' 'libexpat' 'libffi' 'liblzma' 'ncurses' 'libopenssl' 'libreadline' 'mpdecimal' 'libsqlite' 'zlib')
  provides=('python3')
  replaces=('python3')

  cd "${srcdir}/Python-${pkgver}"
  make DESTDIR="${pkgdir}" EXTRA_CFLAGS="$CFLAGS" install

  # Why are these not done by default...
  ln -sf python3               "${pkgdir}"/usr/bin/python.exe
  ln -sf python3-config        "${pkgdir}"/usr/bin/python-config
  ln -sf idle3                 "${pkgdir}"/usr/bin/idle
  ln -sf pydoc3                "${pkgdir}"/usr/bin/pydoc
  ln -sf python${_pybasever}.1 "${pkgdir}"/usr/share/man/man1/python3.1
  ln -sf python${_pybasever}.1 "${pkgdir}"/usr/share/man/man1/python.1

  # Fix FS#22552
  # cp -f "${pkgdir}"/usr/lib/python${_pybasever}/config-${_pybasever}-*/libpython${_pybasever}.dll.a "${pkgdir}"/usr/lib/libpython${_pybasever}.dll.a

  # Clean-up reference to build directory
  # sed -i "s|${srcdir}/Python-${pkgver}:||" "${pkgdir}/usr/lib/python${_pybasever}/config-${_pybasever}-*/Makefile"

  # License
  install -Dm644 LICENSE "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE"

  # fix permissons
  find ${pkgdir}/usr -type f \( -name *.dll -o -name *.exe \) | xargs chmod 0755
}

package_python-devel() {
  pkgdesc="Python headers and dev dependencies"
  depends=("python=${pkgver}" 'libcrypt-devel')
}
